﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
namespace BanHangCshap
{
    public static class BakupRestoreDatabase
    {
        public static Boolean BakupDatabase(string strconn, string DatabaseName, string FileName)
        {
            Boolean ok = true;
            string strBak = "BACKUP DATABASE " + DatabaseName + " TO DISK ='" + FileName + "'";

            try
            {
                SqlConnection sqlconn = new SqlConnection(strconn);
                sqlconn.Open();
                SqlCommand cmd = new SqlCommand(strBak, sqlconn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {

                //MessageBox.Show(ex.ToString());
                ok = false;
            }
            return ok;
        }
        public static Boolean RestoreDatabase(string strconn, string DatabaseName, string FileName)
        {
            Boolean ok = true;
            try
            {
                string strRes = "RESTORE DATABASE " + DatabaseName + " FROM DISK='" + FileName + "' WITH RECOVERY";
                //Connection();

                //this.Connstr = "Server=" + System.Net.Dns.GetHostName() + ";integrated security=SSPI;DataBase=Master;";
                //Connection();
                SqlConnection sqlconn = new SqlConnection(strconn);
                sqlconn.Open();
                SqlCommand cmd = new SqlCommand(strRes, sqlconn);

                cmd.ExecuteNonQuery();
                //MessageBox.Show("Đã phục hồi thành công", "Phục hồi cơ sở dữ liệu", MessageBoxButtons.OK,MessageBoxIcon.Information);

            }
            catch (Exception)
            {
                ok = false;
                //MessageBox.Show("Không thể phục hồi do xảy ra lỗi: " + ex.ToString(), "Phục hồi cơ sở dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return ok;
        }
        public static Boolean DeleteDatabase(string strconn, string DatabaseName)
        {
            Boolean ok = true;
            string strDel = "DROP DATABASE " + DatabaseName;


            try
            {
                SqlConnection sqlconn = new SqlConnection(strconn);
                sqlconn.Open();
                SqlCommand cmd = new SqlCommand(strDel, sqlconn);
                cmd.ExecuteNonQuery();

                //MessageBox.Show("Delete is complete", "Information!!!", MessageBoxButtons.OK);
            }
            catch (Exception)
            {
                ok = false;
                //MessageBox.Show(ex.ToString());
            }
            return ok;
        }
        public static DataTable getAllDatabase(string strconn)
        {

            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT [name] FROM [master].[dbo].[sysdatabases]";
                SqlConnection sqlconn = new SqlConnection(strconn);
                sqlconn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, sqlconn);
                da.Fill(dt);
            }
            catch (Exception)
            {
                dt = null;

            }
            return dt;
        }
        //Kiem tra da cai dat sql express chua
        public static bool IsExpressInstalled()
        {
            using (RegistryKey Key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Microsoft SQL Server\\", false))
            {
                if (Key == null) return false;
                string[] strNames;
                strNames = Key.GetSubKeyNames();

                //If we cannot find a SQL Server registry key, we don't have SQL Server Express installed
                if (strNames.Length == 0) return false;

                foreach (string s in strNames)
                {
                    if (s.StartsWith("MSSQL."))
                    {
                        //Check to see if the edition is "Express Edition"
                        using (RegistryKey KeyEdition = Key.OpenSubKey(s.ToString() + "\\Setup\\", false))
                        {
                            if ((string)KeyEdition.GetValue("Edition") == "Express Edition")
                            {
                                //If there is at least one instance of SQL Server Express installed, return true
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
}
